/* Copyright  2009
   @Author
   Richard Changde Yin            e-mail yinchangde@hotmail.com

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; version 2 of the License.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

/**
 *@filename: hash.h
 */
/* Dynamic hashing of record with different key-length */

#ifndef __COLLAPSAR_HASH_H__
#define __COLLAPSAR_HASH_H__

//#include <my_sys.h>

#ifdef __cplusplus
extern "C" {
#endif

/*
  Overhead to store an element in hash
  Can be used to approximate memory consumption for a hash
 */
#define HASH_OVERHEAD (sizeof(char*)*2)

/* flags for hash_init */
#define HASH_UNIQUE     1       /* hash_insert fails on duplicate key */

typedef unsigned char *(*hash_get_key)(const unsigned char *,size_t*,bool);
typedef void (*hash_free_key)(void *);

typedef struct st_hash {
  /* Length of key if const length */
  size_t key_offset    ;
  size_t key_length    ;
  size_t blength       ;
  uint32_t records     ;
  uint32_t flags       ;
  /* Place for hash_keys */
  DYNAMIC_ARRAY array  ;
  hash_get_key  get_key;
  hash_free_key free   ;
  const CHARSET_INFO *charset;
} HASH;

typedef struct st_hash_info {
  /* index to next key */
  uint32_t next;
  /* data for current entry */
  unsigned char *data;
} HASH_LINK;

/* A search iterator state */
typedef uint32_t HASH_SEARCH_STATE;

bool
_hash_init(HASH *hash,uint32_t growth_size,
           const CHARSET_INFO * const charset,
           uint32_t size, size_t key_offset, size_t key_length,
           hash_get_key get_key,
           hash_free_key free_element, uint32_t flags);
#define hash_init(A,B,C,D,E,F,G,H) _hash_init(A,0,B,C,D,E,F,G,H)
#define hash_init2(A,B,C,D,E,F,G,H,I) _hash_init(A,B,C,D,E,F,G,H,I)
void hash_free(HASH *tree);
void hash_reset(HASH *hash);
unsigned char *hash_element(HASH *hash,uint32_t idx);
unsigned char *hash_search(const HASH *info, const unsigned char *key,
                           size_t length);
unsigned char *hash_first(const HASH *info, const unsigned char *key,
                          size_t length, HASH_SEARCH_STATE *state);
unsigned char *hash_next(const HASH *info, const unsigned char *key,
                         size_t length, HASH_SEARCH_STATE *state);
bool hash_insert(HASH *info,const unsigned char *data);
bool hash_delete(HASH *hash,unsigned char *record);
bool hash_update(HASH *hash,unsigned char *record,
                            unsigned char *old_key,
                            size_t old_key_length);
void hash_replace(HASH *hash, HASH_SEARCH_STATE *state, unsigned char *new_row);

#define hash_clear(H)   memset((H), 0, sizeof(*(H)))
#define hash_inited(H) ((H)->array.buffer != 0)
#define hash_init_opt(A,B,C,D,E,F,G,H)                                  \
  (!hash_inited(A) && _hash_init(A,0,B,C,D,E,F,G, H))

#ifdef __cplusplus
}
#endif  // __cplusplus
#endif  //__COLLAPSAR_HASH_H__
